package myservlet.control;
//管理员查询用户的订单信息
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.sql.rowset.CachedRowSet;
import javax.sql.rowset.RowSetFactory;
import javax.sql.rowset.RowSetProvider;

import mybean.data.DataByPage;

public class SearchOrder extends HttpServlet{
	CachedRowSet rowset = null;
	public void init(ServletConfig config) throws ServletException {
		// TODO Auto-generated method stub
			super.init(config);
			try {//加载驱动
				Class.forName("com.mysql.jdbc.Driver");				
			}
			catch(Exception e) {}
		}
	public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.setCharacterEncoding("utf-8");
		resp.setContentType("text/html;charset=UTF-8");
		PrintWriter out = resp.getWriter();
		String searchMess=req.getParameter("searchMess"),
				radioMess=req.getParameter("radio");
		System.out.println(searchMess);
		System.out.println(radioMess);
		if(radioMess==null) {
			out.println("<script language = javascript>alert('请选择你的查询条件!');");
			out.println("location.href='Ahomepage.jsp'</script>");
			return;
		}
		if((searchMess==null || searchMess.length()==0) && !radioMess.equals("all")) {
			out.println("<script language = javascript>alert('请输入搜索信息!');");
			out.println("location.href='Ahomepage.jsp'</script>");
			return;
		}
		String condition="";
		if(radioMess.equals("id"))//按订单编号查询
			 condition = "select * from orderform where id='"+searchMess+"'";
		else if(radioMess.equals("bid")) {//按单车编号查询
			condition="select * from orderform where B_id='"+searchMess+"'";
		}
		else if(radioMess.equals("name")) {//按用户名查询
			condition="select * from orderform where username='"+searchMess+"'";
		}
		else if(radioMess.equals("all")) {//查询所有单车
			condition="select * from orderform";
		}
		HttpSession session= req.getSession(true);
		Connection con;
		DataByPage dataBean=null;
		try {
			dataBean = (DataByPage)session.getAttribute("dataBean");
			if(dataBean==null) {
				dataBean = new DataByPage();//创建JavaBean对象
				session.setAttribute("dataBean", dataBean);
			}
		}
		catch(Exception EE) {
			dataBean = new DataByPage();
			session.setAttribute("dataBean", dataBean);
		}
		//连接数据库
		String url = "jdbc:mysql://127.0.0.1:3306/bike?&useSSL=false&serverTimezone=GMT%2B8";
		try {
			con = DriverManager.getConnection(url,"root","744615");
			//System.out.println("连接数据库成功");
			Statement sql=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
			ResultSet rs=sql.executeQuery(condition);
			RowSetFactory factory = RowSetProvider.newFactory();
			CachedRowSet rowset = factory.createCachedRowSet();//创建行集对象
			System.out.println(dataBean.getRowSet());
			rowset.populate(rs);//将ResultSet放入CachedRowSet中// 设置从第几条记录开始
			dataBean.setRowSet(rowset);//行集数据存储在dataBean中
			con.close();//关闭连接
		}
		catch (SQLException exp){
			System.out.println(exp);
		}
		resp.sendRedirect("lookorders.jsp");//显示结果
	}
	public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(req, resp);
	}
}
